package com.child.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.child.java.page.PageInfo;
import com.child.java.pojo.ChildWearVO;
import com.child.java.pojo.OutWearOrderVO;
import com.child.java.service.ChildWearService;
import com.child.java.service.OutWearOrderService;
import com.child.util.UUIDGenerter;

@Controller
public class OutWearOrderController {

	@Autowired
	OutWearOrderService outWearOrderService;
	
	@Autowired
	ChildWearService childWearService;
	
	@RequestMapping("/outWearOrder/listAll")
	public String list(ModelMap map,HttpServletRequest request){
		String wearCode = request.getParameter("wearCode");
		Integer pageNo = 1;
		if(request.getParameter("pageNo")!=null&&request.getParameter("pageNo").length()>0) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("wearCode", wearCode);
		
		int totalRecord = outWearOrderService.getListCount(m);
		PageInfo<OutWearOrderVO> pageInfo = new PageInfo<OutWearOrderVO>(totalRecord, 10, pageNo);
		m.put("limitIndex", (pageNo-1)*pageInfo.getPageSize());
		m.put("pageSize", pageInfo.getPageSize());
		
		List<OutWearOrderVO> list = outWearOrderService.getListByPageInfo(m);
		map.put("list", list);
		map.put("pageInfo", pageInfo);
		return "/manager/outWearOrder/outWearOrderList";
	}
	
	@RequestMapping("/outWearOrder/showAdd")
	public String showAdd(ModelMap map){
		return "redirect:/childWear/listAll?urlControl=outWearOrderChoose";
	}
	
	@RequestMapping("/outWearOrder/showUpdate")
	public String showUpdate(@RequestParam("id") String id,ModelMap map){
		OutWearOrderVO OutWearOrder = outWearOrderService.getById(id);
		map.put("OutWearOrder", OutWearOrder);
		return "/manager/outWearOrder/outWearOrderEdit";
	}
	
	@RequestMapping("/outWearOrder/save")
	public String add(HttpServletRequest request,ModelMap map){
		OutWearOrderVO outWearOrder = new OutWearOrderVO();
		outWearOrder.setId(UUIDGenerter.getUUID());
		outWearOrder.setWearId(request.getParameter("id"));
		outWearOrder.setOutWearDate(request.getParameter("outWearDate"));
		outWearOrder.setOutNumber(Integer.parseInt(request.getParameter("outNumber")));
		outWearOrder.setOutPrice(Double.parseDouble(request.getParameter("outPrice")));
		outWearOrder.setOutPriceSum(Double.parseDouble(request.getParameter("outPriceSum")));
		if(request.getParameter("id")!=null) {
			Map<String,Object> m = new HashMap<String, Object>();
			String id = request.getParameter("id");
			Integer count = Integer.parseInt(request.getParameter("outNumber"));
			m.put("id", id);
			m.put("count", count);
			childWearService.cutAmount(m);
			
		}
		outWearOrderService.insert(outWearOrder);
		
		return "redirect:/outWearOrder/listAll";
	}
	
	@RequestMapping("/outWearOrder/delete")
	public String add(@RequestParam("id") String id,ModelMap map){
		outWearOrderService.delete(id);
		return "redirect:/outWearOrder/listAll";
	}

	@RequestMapping("/outWearOrder/next")
	public String next(@RequestParam("id") String id,ModelMap map){
		ChildWearVO childWearVO = childWearService.getById(id);
		map.put("childWear", childWearVO);
		return "/manager/outWearOrder/outWearOrderEdit";
	}
	
	
	
}
